package sector.soda.util;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

/**
 *
 * @author cheesecake
 */
public class MyLogger extends Handler {

    FileOutputStream fileOutputStream;
    PrintWriter printWriter;

    public MyLogger(String filename) {
        super();
        
        if (filename == null || filename.isEmpty()) {
            filename = "MyLogger.log";
        }

        try {
            fileOutputStream = new FileOutputStream(filename);
            printWriter = new PrintWriter(fileOutputStream);
            setFormatter(new SimpleFormatter());
        } catch (Exception e) {
            // handle Exception
        }
    }
    
    public void log(Level level, String msg) {
        SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        Date now = new Date();
        
        printWriter.println("[" + level.toString() + "] " + df.format(now) + ": " + msg + "\n");
        printWriter.flush();
    }

    @Override
    public void publish(LogRecord record) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void flush() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void close() throws SecurityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}